﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;

namespace HZ.Supervise.Web
{
    public class Function
    {

        public static HZ.Supervise.IBLL.IBLL_User ibllEntity = BLLFactory.BLL_User();

        /// <summary>
        /// 格式化采购类型
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string FormatDrugSource(object obj)
        {
            string status = "全部";
            if (obj != null)
            {
                switch (Convert.ToInt16(obj))
                {

                    //2011-0329 ml修改采购类型为安徽
                    case 0: status = "日常采购";
                        break;
                    case 1: status = "临时采购";
                        break;
                    case 2: status = "应急采购";
                        break;
                }
            }
            return status;
        }

        public static string FormatDrugSourceString(object obj)
        {
            string status = "全部";
            if (obj != null)
            {
                if (obj.ToString().Trim() == "2010基本药物目录")
                {
                    status = "<font color=black>2010基本药物目录</font>";
                }
                if (obj.ToString().Trim() == "2010限价品种")
                {
                    status = "<font color=red>2010限价品种</font>";
                }
                if (obj.ToString().Trim() == "2010补充目录")
                {
                    status = "<font color=blue>2010补充目录</font>";
                }

            }
            return status;
        }

        /// <summary>
        /// 安徽采购类型 
        /// 肖梦兰
        /// 20110409
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string FormatDrugSourceByName(object obj)
        {
            string status = "全部";
            if (obj != null)
            {
                switch (Convert.ToInt16(obj))
                {
                    case 0: status = "日常采购";
                        break;
                    case 1: status = "临时采购";
                        break;
                    case 2: status = "应急采购";
                        break;
                }
            }
            return status;
        }

        /// <summary>
        /// 采购范围：0：县及县以上可用；1：全省
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string PurchaseRange(object obj)
        {
            string status = "未知";
            if (obj != null)
            {
                switch (Convert.ToInt16(obj))
                {
                    case 0: status = "<font color=black>县及县以上可用</font>";
                        break;
                    case 1: status = "<font color=black>全省</font>";
                        break;
                }
            }
            return status;
        }
        /// <summary>
        /// 调价作用范围：0：全区域；1：当前区
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string Isdistrict(object obj)
        {
            string status = "未知";
            if (obj != null)
            {
                switch (Convert.ToInt16(obj))
                {
                    case 1: status = "<font color=black>当前区</font>";
                        break;
                    case 0: status = "<font color=red>全区域</font>";
                        break;

                }
            }
            return status;
        }
        /// <summary>
        /// 获得排名变化图片
        /// </summary>
        /// <param name="numb">大于0 绿色 小于 0 红色  </param>
        /// <returns></returns>
        public static string GetImageurl(object numbn)
        {
            int numb = int.Parse(numbn.ToString());
            if (numb == 0)
            {
                return "/lib/images/ARW05LT.gif";
            }
            if (numb > 0)
            {
                return "/lib/images/ARW06UP.gif";
            }
            if (numb < 0)
            {
                return "/lib/images/ARW05DN.gif";
            }
            return "";
        }
        /// <summary>
        /// 格式化采购时间
        /// </summary>
        /// <param name="strplantime">时间名称 如：PlanTime </param>
        /// <param name="obj"> 如：200904</param>
        /// <returns> 如( PlanTime>='2009-04-01' and PlanTime<'2009-05-01') </returns>
        public static string FormatPlanTime(string strplantime, object obj)
        {
            if (obj != null && obj.ToString() != "")
            {
                string strtime = obj.ToString();
                string strtimestar = strtime.Substring(0, 4) + "-" + strtime.Substring(4, 2) + "-01";
                string strtimeend = DateTime.Parse(strtimestar).AddMonths(1).ToString("yyyy-MM-dd");
                return "(" + strplantime + ">='" + strtimestar + "' and " + strplantime + "<'" + strtimeend + "')";
            }
            return "";
        }

        /// <summary>
        /// 格式化采购时间[上个月时间] 对比是用
        /// </summary>
        /// <param name="strplantime">时间名称 如：PlanTime </param>
        /// <param name="obj"> 如：200904</param>
        /// <returns> 如( PlanTime>='2009-04-01' and PlanTime<'2009-05-01') </returns>
        public static string FormatPlanTime_last(string strplantime, object obj)
        {
            if (obj != null && obj.ToString() != "")
            {
                string strtime = obj.ToString();
                string strtimestar = strtime.Substring(0, 4) + "-" + strtime.Substring(4, 2) + "-01";
                string strtimeend = DateTime.Parse(strtimestar).AddMonths(-1).ToString("yyyy-MM-dd");
                return "(" + strplantime + ">='" + strtimeend + "' and " + strplantime + "<'" + strtimestar + "')";
            }
            return "";
        }

        /// <summary>
        /// 格式化采购时间[下个月时间] 对比是用
        /// </summary>
        /// <param name="strplantime">时间名称 如：PlanTime </param>
        /// <param name="obj"> 如：200904</param>
        /// <returns> 如( PlanTime>='2009-04-01' and PlanTime<'2009-05-01') </returns>
        public static string FormatDayTime(string strplantime, object obj)
        {
            if (obj != null && obj.ToString() != "")
            {
                string strtime = obj.ToString();
                string strtimestar = strtime.Substring(0, 4) + "-" + strtime.Substring(4, 2) + "-01";
                string strtimeend = DateTime.Parse(strtimestar).AddMonths(1).ToString("yyyy-MM-dd");
                //return "(" + strplantime + ">='" + strtimeend + "' and " + strplantime + "<'" + strtimestar + "')";
                return strtimeend.Replace("-", "").Substring(0, 6);
            }
            return "";
        }

        /// <summary>
        /// 格式化时间，用于对比统计。上个方法不适用于对比统计，固而新加
        /// 董辉
        /// 2011-4-18 
        /// </summary>
        /// <param name="strplantime"></param>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string FormatDayTimeByDH(string strplantime, object obj)
        {
            if (obj != null && obj.ToString() != "")
            {
                string strtime = obj.ToString();

                string strtimestar = strtime.Substring(0, 4) + "-" + strtime.Substring(4, 2) + "-01";

                string currentTime = DateTime.Parse(strtimestar).AddMonths(-1).ToString("yyyy-MM-dd");

                return currentTime.Replace("-", "").Substring(0, 6);
            }

            return "";
        }

        /// <summary>
        /// 截取字符串
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string SubStr(object obj)
        {
            string substring = "";
            if (obj != null)
            {
                int length = obj.ToString().Length;
                if (length > 20)
                {
                    substring = obj.ToString().Substring(0, 20) + "...";
                }
                else
                {
                    substring = obj.ToString();
                }
            }
            return substring;
        }

        /// <summary>
        /// 获取指定周的范围
        /// </summary>
        /// <param name="weekofyeay"></param>
        /// <param name="datefrom"></param>
        /// <param name="dateto"></param>
        public static string GetWeekRange(object weekofyeay)
        {
            if (HZ.Fun.IsInt(weekofyeay.ToString()))
            {
                int nweekofyeay = Convert.ToInt32(weekofyeay);
                DateTime date = new DateTime(2010, 1, 1);
                date = HZ.Fun2.GetNextSaturday(date);

                DateTime datefrom = date.AddDays((nweekofyeay - 2) * 7 + 1);   //本周周一
                DateTime dateto = datefrom.AddDays(6);  //本周周日

                string strreturn = datefrom.ToString() + "--" + dateto.ToString();
                return strreturn;
            }
            else
                return "";
        }
        /// <summary>
        /// 字符串数字是否包含字符串
        /// </summary>
        /// <param name="strzz"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsContains(string[] strzz, string str)
        {
            for (int i = 0; i < strzz.Length; i++)
            {
                if (strzz[i].Equals(str)) return true;
            }
            return false;
        }

        public static int GetColumnMerge(DataTable dt, int j)
        {
            if (dt.Rows[j][0].ToString().Trim() == dt.Rows[j + 1][0].ToString().Trim())
            {

            }
            return 2;
        }


        /// <summary>
        /// 曹鹏
        /// 2011年4月26日18:20:00
        /// 权限控制，若一个页面无权限，则隐藏菜单
        /// </summary>
        /// <param name="menuName"></param>
        /// <returns></returns>
        public static string CheckMenuByUserID(string menuName)
        {

            if (CurrentAccount.GetUser().UserID == "root")//如果是超级管理员，权限全开
            {

                return "";

            }
            else
            {
                IList<HZ.Supervise.Model.OperatingRule> list = ibllEntity.GetUserOperatingRule(CurrentAccount.GetUser().UserID);//得到该用户拥有的所有权限

                string flag = "display:none";

                foreach (HZ.Supervise.Model.OperatingRule itemMenu in list)//遍历
                {

                    if (itemMenu.OperatingRuleName == menuName)//匹配
                    {

                        flag = "";//存在的话，返回一个空字符串，菜单显示

                        break;//终止循环

                    }

                }

                return flag;
            }

        }
        /// <summary>
        /// 曹鹏
        /// 2011年4月27日10:18:53
        /// 当地区是县级地区时，应该显示成某某市某某县
        /// </summary>
        /// <param name="contryName">地区名称</param>
        /// <returns>地区完整的名称</returns>
        public static string GetCityNameByCountryName(string contryName)
        {

            string flag = "";

            if (contryName.Contains("县"))//如果该地区是个县，那么应该改成完整的名称
            {

                HZ.Supervise.IBLL.IBLL_Area ibllEntity = new HZ.Supervise.BLL.BLL_Area();

                string strCityName = ibllEntity.GetCityNameByCountryName(contryName);//得到该县所属的市

                flag = strCityName + contryName;//拼接

            }
            else
            {

                flag = contryName;//其他情况，不做改变，直接返回原始值

            }

            return flag;
        }


        public static string GetCityNameByCountry(string contryName)
        {

            string flag = "";

            if (contryName.Contains("县"))//如果该地区是个县，那么应该改成完整的名称
            {

                HZ.Supervise.IBLL.IBLL_Area ibllEntity = new HZ.Supervise.BLL.BLL_Area();

                string strCityName = ibllEntity.GetCityNameByCountryName(contryName);//得到该县所属的市

                flag = strCityName + contryName;//拼接

            }
            else if (contryName.Contains("区"))
            {
                HZ.Supervise.IBLL.IBLL_Area ibllEntity = new HZ.Supervise.BLL.BLL_Area();

                string strCityName = ibllEntity.GetCityNameByCountryName(contryName);//得到该县所属的市

                flag = strCityName + contryName;//拼接
            }
            else
            {

                flag = contryName;//其他情况，不做改变，直接返回原始值

            }

            return flag;
        }

        /// <summary>
        /// 合并单元格(默认合并的是第一列)
        /// </summary>
        /// <param name="gv"></param>
        public static void Combine(GridView gv)
        {
            int m;

            for (int i = 0; i < gv.Rows.Count; )//遍历
            {
                m = 0;//初始化变量

                for (int j = i + 1; j < gv.Rows.Count; j++)//内层循环
                {
                    if (gv.Rows[i].Cells[0].Text == gv.Rows[j].Cells[0].Text)//
                    {

                        gv.Rows[j].Cells[0].Visible = false;//隐藏相同的单元格

                        m++;

                    }
                    else
                    {
                        break;
                    }
                }

                gv.Rows[i].Cells[0].RowSpan = m + 1;//单元格合并

                i = i + m + 1;//给i赋值

            }
        }
        /// <summary>
        /// 曹鹏
        /// 2011年5月8日18:45:48
        /// 根据其中一列合并另外一列的单元格（重载）
        /// </summary>
        /// <param name="gv"></param>
        /// <param name="baseIndex">所要依据的单元格所在列索引</param>
        /// <param name="index">要合并的列的索引</param>
        public static void Combine(GridView gv, int baseIndex, int combineindex)
        {
            int m;

            for (int i = 0; i < gv.Rows.Count; )//遍历
            {
                m = 0;//初始化变量

                for (int j = i + 1; j < gv.Rows.Count; j++)//内层循环
                {
                    if (gv.Rows[i].Cells[baseIndex].Text == gv.Rows[j].Cells[baseIndex].Text)
                    {

                        gv.Rows[j].Cells[combineindex].Visible = false;//隐藏相同的单元格

                        m++;

                    }
                    else
                    {

                        break;

                    }
                }

                gv.Rows[i].Cells[combineindex].RowSpan = m + 1;//单元格合并

                i = i + m + 1;//给i赋值

            }
        }


        /// <summary>
        /// 曹鹏 2011年5月4日15:35:32 给DataTable加一列（特定用于采购类型列）
        /// </summary>
        /// <param name="dt">数据源</param>
        ///<param name="columnName">采购类型列</param>
        public static void AddColumn(DataTable dt, string columnName)
        {

            DataColumn dc = new DataColumn("addDataColumn");//创建对象

            dt.Columns.Add(dc);//加到datatable中

            dc.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

            dc.DataType = typeof(string);//设置该列的数据类型

            int i = 0;

            foreach (DataRow item in dt.Rows)//遍历
            {

                i = Convert.ToInt32(item[columnName]);

                switch (i)
                {

                    case 0:

                        item["addDataColumn"] = "日常采购";

                        break;
                    case 1:

                        item["addDataColumn"] = "临时采购";

                        break;
                    case 2:

                        item["addDataColumn"] = "应急采购";

                        break;
                    default:

                        item["addDataColumn"] = "全部";

                        break;

                }
            }
        }


        /// <summary>
        /// by张雄  2011.6.23
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="columnName1">入库率列</param>
        /// <param name="columnName2">采购类型列</param>
        public static void AddColumn(DataTable dt, string columnRate, string columnSortSource)
        {

            DataColumn dc1 = new DataColumn("addDataColumnRate");//创建对象

            dt.Columns.Add(dc1);//加到datatable中

            dc1.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

            dc1.DataType = typeof(string);//设置该列的数据类型

            DataColumn dc2 = new DataColumn("addDataColumn");//创建对象

            dt.Columns.Add(dc2);//加到datatable中

            dc2.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

            dc2.DataType = typeof(string);//设置该列的数据类型

            int j = 0;

            for (int i = 0; i < dt.Rows.Count; i++)
            {

                dt.Rows[i]["addDataColumnRate"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate]));//格式化小数，变为百分数

                j = Convert.ToInt32(dt.Rows[i][columnSortSource]);

                switch (j)
                {

                    case 0:

                        dt.Rows[i]["addDataColumn"] = "日常采购";

                        break;
                    case 1:

                        dt.Rows[i]["addDataColumn"] = "临时采购";

                        break;
                    case 2:

                        dt.Rows[i]["addDataColumn"] = "应急采购";

                        break;
                    default:

                        dt.Rows[i]["addDataColumn"] = "全部";

                        break;

                }
            }

		}

		/// <summary>
		/// ml 2011-0505 修改入库率
		/// </summary>
		/// <param name="dt">数据源</param>
		/// <param name="columnRate">入库率列明</param>
        //public static void ChangeRate(DataTable dt, string columnRate)
        //{
        //    DataColumn dc1 = new DataColumn("addDataColumnRate");//创建对象

        //    dt.Columns.Add(dc1);//加到datatable中

        //    dc1.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

        //    dc1.DataType = typeof(string);//设置该列的数据类型        



        //    for (int i = 0; i < dt.Rows.Count; i++)
        //    {

        //        dt.Rows[i]["addDataColumnRate"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate]));//格式化小数，变为百分数               

        //    }
        //}

        //        dt.Rows[i][columnNameByYou] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate]));//格式化小数，变为百分数               

        //    }
        //}

		/// <summary>
		/// 配送率
		/// by张雄  2011.5.5
		/// </summary>
		/// <param name="dt"></param>
		/// <param name="columnRate_PS"></param>
		public static void ChangeRate_PS(DataTable dt, string columnRate_PS)
		{
			DataColumn dc_PS = new DataColumn("DataRate_PS");   //创建一个列

			dt.Columns.Add(dc_PS);  //添加这个列到Table

			dc_PS.SetOrdinal(dt.Columns.Count - 1); //设置该列的索引

			dc_PS.DataType = typeof(string);    //设置该列的类型



			for (int i = 0; i < dt.Rows.Count; i++) //遍历表行
			{
				dt.Rows[i]["DataRate_PS"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate_PS] == DBNull.Value ? 0.00 : dt.Rows[i][columnRate_PS]));
			}
		}


        /// <summary>
        /// 修改销售率
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="columnRate">入库率列明</param>
        public static void ChangeRate_Sell(DataTable dt, string columnRate)
        {
            DataColumn dc1 = new DataColumn("addSellRate");//创建对象

            dt.Columns.Add(dc1);//加到datatable中

            dc1.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

            dc1.DataType = typeof(string);//设置该列的数据类型        



            for (int i = 0; i < dt.Rows.Count; i++)
            {

                dt.Rows[i]["addSellRate"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate]));//格式化小数，变为百分数               

            }
        }

		/// <summary>
		/// 曹鹏 2011年5月5日18:20:36 将再datatable中增加一列，该列显示的是指定列中值（小数）的百分比形式
		/// </summary>
		/// <param name="dt">数据源</param>
		/// <param name="columnRate">数据源上指定的列名</param>
		/// <param name="columnNameByYou">指定增加列的列名</param>
        //public static void ChangeRate(DataTable dt, string columnRate, string columnNameByYou)
        //{
        //    DataColumn dc1 = new DataColumn(columnNameByYou);//创建对象

        //    dt.Columns.Add(dc1);//加到datatable中

        //    dc1.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

        //    dc1.DataType = typeof(string);//设置该列的数据类型   
     

        /// <summary>
        /// ml 2011-0505 修改入库率
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="columnRate">入库率列明</param>
        public static void ChangeRate(DataTable dt, string columnRate)
        {
            DataColumn dc1 = new DataColumn("addDataColumnRate");//创建对象

            dt.Columns.Add(dc1);//加到datatable中

            dc1.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

            dc1.DataType = typeof(string);//设置该列的数据类型        



            for (int i = 0; i < dt.Rows.Count; i++)
            {

                dt.Rows[i]["addDataColumnRate"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate]));//格式化小数，变为百分数               

            }
        }

        /// <summary>
        /// 曹鹏 2011年5月5日18:20:36 将再datatable中增加一列，该列显示的是指定列中值（小数）的百分比形式
        /// </summary>
        /// <param name="dt">数据源</param>
        /// <param name="columnRate">数据源上指定的列名</param>
        /// <param name="columnNameByYou">指定增加列的列名</param>
        public static void ChangeRate(DataTable dt, string columnRate, string columnNameByYou)
        {
            DataColumn dc1 = new DataColumn(columnNameByYou);//创建对象

            dt.Columns.Add(dc1);//加到datatable中

            dc1.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

            dc1.DataType = typeof(string);//设置该列的数据类型        


            for (int i = 0; i < dt.Rows.Count; i++)
            {

                dt.Rows[i][columnNameByYou] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate]));//格式化小数，变为百分数               

            }
        }

        /// <summary>
        /// 配送率
        /// by张雄  2011.5.5
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="columnRate_PS"></param>
        //public static void ChangeRate_PS(DataTable dt, string columnRate_PS)
        //{
        //    DataColumn dc_PS = new DataColumn("DataRate_PS");   //创建一个列

        //    dt.Columns.Add(dc_PS);  //添加这个列到Table

        //    dc_PS.SetOrdinal(dt.Columns.Count - 1); //设置该列的索引

        //    dc_PS.DataType = typeof(string);    //设置该列的类型



        //    for (int i = 0; i < dt.Rows.Count; i++) //遍历表行
        //    {
        //        dt.Rows[i]["DataRate_PS"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate_PS] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate_PS]));
        //    }
        //}


        /// <summary>
        /// 配送金额
        /// by张雄
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="columnPS">配送数量</param>
        /// <param name="columnPrice">单价</param>
        public static void ChangePSJE(DataTable dt, string columnPS, string columnPrice)
        {
            DataColumn dc1 = new DataColumn("addDataColumnPSJE");//创建对象

            dt.Columns.Add(dc1);//加到datatable中

            dc1.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引

            dc1.DataType = typeof(string);//设置该列的数据类型        



            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //<%#(Convert.ToDecimal(DataBinder.Eval(Container,"DataItem.DistributeCount")) * Convert.ToDecimal(DataBinder.Eval(Container,"DataItem.Price"))) %>
                dt.Rows[i]["addDataColumnPSJE"] = Convert.ToDecimal(dt.Rows[i][columnPS]) * Convert.ToDecimal(dt.Rows[i][columnPrice]);
            }
        }


        /// <summary>
        /// 转换多行，配送率、入库率等
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="columnRate_PS"></param>
        public static void ChangeRate_PS_More(DataTable dt, string columnRate_PS, string column_rk)
        {
            DataColumn dc_PS = new DataColumn("DataRate_PS");   //创建一个列-配送率

            dt.Columns.Add(dc_PS);  //添加这个列到Table

            dc_PS.SetOrdinal(dt.Columns.Count - 1); //设置该列的索引

            dc_PS.DataType = typeof(string);    //设置该列的类型

            DataColumn addDataColumnRate = new DataColumn("addDataColumnRate");   //创建一个列-入库率

            dt.Columns.Add(addDataColumnRate);  //添加这个列到Table

            addDataColumnRate.SetOrdinal(dt.Columns.Count - 1); //设置该列的索引

            addDataColumnRate.DataType = typeof(string);    //设置该列的类型

            for (int i = 0; i < dt.Rows.Count; i++) //遍历表行
            {
                dt.Rows[i]["DataRate_PS"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate_PS] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate_PS]));
                dt.Rows[i]["addDataColumnRate"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][column_rk] == DBNull.Value ? 0.000 : dt.Rows[i][column_rk]));

            }
        }



        /// <summary>
        /// 入库率
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="columnRate_PS"></param>
        public static void ChangeRate_rk(DataTable dt, string columnRate_rk)
        {
            DataColumn dc_PS = new DataColumn("addDataColumnRate");   //创建一个列

            dt.Columns.Add(dc_PS);  //添加这个列到Table

            dc_PS.SetOrdinal(dt.Columns.Count - 2); //设置该列的索引

            dc_PS.DataType = typeof(string);    //设置该列的类型



            for (int i = 0; i < dt.Rows.Count; i++) //遍历表行
            {
                dt.Rows[i]["addDataColumnRate"] = string.Format("{0:P}", Convert.ToDecimal(dt.Rows[i][columnRate_rk] == DBNull.Value ? 0.000 : dt.Rows[i][columnRate_rk]));
            }
        }

        /// <summary>
        /// 曹鹏
        /// 2011年5月5日15:43:10
        /// 再第二次绑定mschart时，直接从gridview上面取数据，不从数据库取，从而提高页面加载速度
        /// </summary>
        /// <param name="gv">gridview控件</param>
        /// <param name="indexOfsortsource">“sortsource”字段绑定列的索引</param>
        /// <param name="indexOfKeyNameOfsortsource">“sortsource”在主键集合中的索引</param>
        public static DataTable InsertDataTable(GridView gv, int indexOfsortsource, int indexOfKeyNameOfsortsource)
        {

            DataTable dtceshi = new DataTable();//定义一个datatable变量

            for (int i = 0; i < gv.Columns.Count; i++)//第一次遍历，目的是给定义的datatable的头赋值
            {
                DataColumn dc = new DataColumn(gv.Columns[i].FooterText);//定义一新列，把绑定在gridview脚模板中的数据字段作为datatable的头

                dc.DataType = typeof(string);//定义列的数据类型

                dtceshi.Columns.Add(dc);//将新列加入到datatable中

            }

            foreach (GridViewRow item in gv.Rows)//第二次遍历，将gridview中每一行加入到datatable中
            {

                DataRow dr = dtceshi.NewRow();//定义一新行

                for (int i = 0; i < item.Cells.Count; i++)//遍历这行中的单元格
                {

                    if (i == indexOfsortsource)//由于sortsource列是隐藏的，无法取到文本，所以当遍历到该列时，我们就从事先绑定在主键中的数据取到sortsource该列的数据
                    {

                        dr[i] = gv.DataKeys[item.RowIndex][indexOfKeyNameOfsortsource].ToString();

                    }
                    else
                    {

                        dr[i] = item.Cells[i].Text;

                    }
                }

                dtceshi.Rows.Add(dr);//把新列加到datatable中

            }

            return dtceshi;//返回
        }

        /// <summary>
        /// 曹鹏 
        /// 2011年5月7日15:34:22
        /// 得到当前时间，确定当前是上午还是下午
        /// </summary>
        /// <returns></returns>
        internal static string GetNowTime()
        {

            int hour = Convert.ToInt32(DateTime.Now.ToString("HH"));//把小时格式化成24小时制，以利于对比
            if (0 <= hour && hour < 12)
            {

                return "上午好!";

            }

            else
            {

                return "下午好!";

            }

        }


        /// <summary>
        /// 格式化日期的时间
        /// by张雄  2011.5.7
        /// </summary>
        /// <param name="Str">未格式化的日期的参数</param>
        /// <returns>格式化后的日期</returns>
        public static string FormartDate(string Str)
        {
            //年变量
            string YearStr = String.Empty;
            //月变量
            string MonthStr = String.Empty;

            if (!string.IsNullOrEmpty(Str))
            {
                YearStr = Str.Substring(0, 4) + "年";       //取得年

                MonthStr = Str.Substring(4, 2);             //取得月
                if (MonthStr.Substring(0, 1) == "0")
                {
                    MonthStr = MonthStr.Substring(1, 1) + "月";
                }
                else
                {
                    MonthStr += "月";
                }
            }
            return Str = YearStr + MonthStr;       //取得完整的时间
        }

        public static string ReturnAreaName(string strAreaId)
        {
            string strSql = "select AreaName from tb_area where areaid=" + strAreaId;
            return HZ.Data.DbHelperFactory.Create().ExecuteScalar(strSql).ToString();
        }
        /// <summary>
        /// 曹鹏
        /// 2011年5月13日11:26:26
        /// 增加一新列（针对排名）
        /// </summary>
        /// <param name="strRank">指定的列名</param>
        /// <param name="dt">数据源</param>
        /// <returns></returns>
        public static DataTable AddColRank(DataTable dt, string strRank)
        {

            DataColumn dc = new DataColumn("addColumnOfRank", typeof(string));

            dt.Columns.Add(dc);

            dc.SetOrdinal(dt.Columns.Count - 1); //设置该列的索引

            for (int i = 0; i < dt.Rows.Count; i++) //遍历表行
            {
                dt.Rows[i]["addColumnOfRank"] = string.IsNullOrEmpty((dt.Rows[i][strRank]).ToString()) ? "无" : dt.Rows[i][strRank];
            }

            return dt;

        }
        /// <summary>
        ///  曹鹏
        ///  2011年5月16日17:31:26
        ///  通过地区ID拼接查询条件
        /// </summary>
        /// <param name="obj">用于拼接字符串的对象类型（string or StringBuilder）,如果传入的是其他类型，会抛出异常</param>
        /// <param name="areid">地区ID</param>
        /// <returns></returns>
        public static Object GetConditionOfAreaId(object obj, string areid)
        {

            if (obj is string)
            {

                string str = (string)obj;

                if (!string.IsNullOrEmpty(areid))
                {
                    if (areid != Constants.CURRENT_DEPLOY_AREAID)
                    {
                        //2011.3.31 张雄 modify
                        //市级 取前4位
                        if (areid.Length == 4)
                        //if (strAreaId.Length == 3)
                        {
                            //市级 取前4位
                            str += " and AreaId like '" + areid + "%'";
                        }
                        else
                        {
                            //县级
                            str += " and AreaId = '" + areid + "'";
                        }
                    }
                    else
                    {
                        str += " and AreaId like '22%'";
                    }
                }
                else
                {
                    HZ.Supervise.Model.User user = CurrentAccount.GetUser();
                    areid = user.Org.Area.AreaID;
                    if (areid.Substring(4, 2).Equals("00"))// 末尾00标示市级
                    {
                        str += " and AreaId like '" + areid.Substring(0, 4) + "%'";
                    }
                    else
                    {
                        str += " and AreaId = '" + areid + "'";
                    }
                }

                return str;

            }

            else if (obj is StringBuilder)
            {

                StringBuilder sb = (StringBuilder)obj;

                if (!string.IsNullOrEmpty(areid))
                {
                    if (areid != Constants.CURRENT_DEPLOY_AREAID)
                    {
                        //2011.3.31 张雄 modify
                        //市级 取前4位
                        if (areid.Length == 4)
                        {
                            //市级 取前4位
                            sb.Append(" and AreaId like '" + areid + "%'");
                        }
                        else
                        {
                            //县级
                            sb.Append(" and AreaId = '" + areid + "'");
                        }
                    }
                    else
                    {
                        sb.Append(" and AreaId like '22%'");
                    }
                }
                else
                {
                    HZ.Supervise.Model.User user = CurrentAccount.GetUser();
                    areid = user.Org.Area.AreaID;
                    if (areid.Substring(4, 2).Equals("00"))// 末尾00标示市级
                    {
                        sb.Append(" and AreaId like '" + areid.Substring(0, 4) + "%'");
                    }
                    else
                    {
                        sb.Append(" and AreaId = '" + areid + "'");
                    }
                }

                return sb;

            }
            else
            {

                throw new Exception("未识别的类型");

            }

        }


        /// <summary>
        /// 页面加载时给文本框赋值当前时间的值
        /// by：张雄
        /// </summary>
        /// <param name="StartTime">上月当前时间</param>
        /// <param name="EndTime">本月当前时间</param>
        public static void SetDatetime(TextBox StartTime, TextBox EndTime)
        {
            EndTime.Text = DateTime.Now.ToString("yyyy-MM-dd");

            StartTime.Text = Convert.ToDateTime(EndTime.Text.Trim()).AddMonths(-1).ToString("yyyy-MM-dd");

        }

        /// <summary>
        /// 基层医疗机构
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="columnName"></param>
        public static void IsCountry(DataTable dt, string columnName)
        {
            DataColumn dc = new DataColumn("Country");//创建对象
            dt.Columns.Add(dc);//加到datatable中
            dc.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引
            dc.DataType = typeof(string);//设置该列的数据类型
            int i = 0;
            foreach (DataRow item in dt.Rows)//遍历
            {
                i = Convert.ToInt32(item[columnName]);
                switch (i)
                {
                    case 0:
                        item["Country"] = "非基层医疗机构";
                        break;
                    case 1:
                        item["Country"] = "乡镇";
                        break;
                    case 2:
                        item["Country"] = "社区";
                        break;
                    case 3:
                        item["Country"] = "民营";
                        break;
                    case 4:
                        item["Country"] = "其他";
                        break;
                    default:
                        item["Country"] = "其他";
                        break;
                }
            }
        }
        /// <summary>
        /// 医院等级
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="columnName"></param>
        public static void Levels(DataTable dt, string columnName)
        {
            DataColumn dc = new DataColumn("Level");//创建对象
            dt.Columns.Add(dc);//加到datatable中
            dc.SetOrdinal(dt.Columns.Count - 1);//设置该列的索引
            dc.DataType = typeof(string);//设置该列的数据类型
            int i = 0;
            foreach (DataRow item in dt.Rows)//遍历
            {
                i = Convert.ToInt32(item[columnName]);
                switch (i)
                {
                    case 0:
                        item["Level"] = "其他";
                        break;
                    case 1:
                        item["Level"] = "一甲";
                        break;
                    case 2:
                        item["Level"] = "一乙";
                        break;
                    case 3:
                        item["Level"] = "二甲";
                        break;
                    case 4:
                        item["Level"] = "二乙";
                        break;
                    case 5:
                        item["Level"] = "三甲";
                        break;
                    case 6:
                        item["Level"] = "三乙";
                        break;
                    default:
                        item["Level"] = "其他";
                        break;
                }
            }
        }

        /// <summary>
        /// 首页图表汇总类型 
        /// JOHN
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string FormatAmountTypeByName(object obj)
        {
            string status = "";
            if (obj != null)
            {
                switch (Convert.ToInt16(obj))
                {
                    case 1: status = "采购总金额";
                        break;
                    case 2: status = "配送总金额";
                        break;
                    case 3: status = "入库总金额";
                        break;
                    case 4: status = "退货总金额";
                        break;
                }
            }
            return status;
        }

        /// <summary>
        /// 返回拼接完成的Sql
        /// AreaID Like '22%'
        /// </summary>
        /// <param name="areaid"></param>
        /// <returns></returns>
        public static string GetAreaIDBySQL(string areaid)
        {
            string tmpAreaid = GetAreaIDBySTR(areaid);
            if (tmpAreaid.Length < 5)
            {
                return string.Format(" AND AreaID LIKE '{0}%'", tmpAreaid);
            }
            else
            {
                return string.Format(" AND AreaID IN ({0})", tmpAreaid);

            }
            //return areaid;

        }
        /// <summary>
        /// 返回AreaID列表
        /// 220202，220203
        /// </summary>
        /// <param name="areaid"></param>
        /// <returns></returns>
        public static string GetAreaIDBySTR(string areaid)
        {
            string tmpAreaid = "";
            if (!string.IsNullOrEmpty(areaid))
            {
                switch (areaid)
                {
                    case "130000":
                        tmpAreaid = "13";
                        break;
                    case "220179":
                        tmpAreaid = "'220100','220102','220103','220104','220105','220106','220184','220185','220186','220187','220188'";
                        break;
                    case "220279":
                        tmpAreaid = "'220200','220202','220203','220204','220211','220285'";
                        break;
                    case "220379":
                        tmpAreaid = "'220300','220302','220303','220324'";
                        break;
                    case "220479":
                        tmpAreaid = "'220400','220402','220403'";
                        break;
                    case "220579":
                        tmpAreaid = "'220500','220502','220503'";
                        break;
                    case "220679":
                        tmpAreaid = "'220600','220682'";
                        break;
                    case "220779":
                        tmpAreaid = "'220700','220702','220797','220798','220799'";
                        break;
                    case "220879":
                        tmpAreaid = "'220800','220802'";
                        break;
                    case "222479":
                        tmpAreaid = "'222400'";
                        break;
                    case "222579":
                        tmpAreaid = "'222500','222501','222502','222503','222599'";
                        break;
                    //case "2279":
                    //    tmpAreaid = "'220112'";
                    //    break;
                    //case "2298":
                    //    tmpAreaid = "'220625'";
                    //    break;
                    default:
                        tmpAreaid = "'" + areaid + "'";
                        break;


                }
                if (areaid != "130000" && areaid.Length == 4)
                {
                    tmpAreaid = areaid;
                }
            }
            else
            {
                tmpAreaid = "13";
            }
            return tmpAreaid;
        }

        public static string GetAreaNameBySTR(string areaid)
        {
            string tmpAreaid = "";

            switch (areaid)
            {
                case "220179":
                    tmpAreaid = "长春市(本级)";
                    break;
                case "220279":
                    tmpAreaid = "吉林市(本级)";
                    break;
                case "220379":
                    tmpAreaid = "四平市(本级)";
                    break;
                case "220479":
                    tmpAreaid = "辽源市(本级)";
                    break;
                case "220579":
                    tmpAreaid = "通化市(本级)";
                    break;
                case "220679":
                    tmpAreaid = "白山市(本级)";
                    break;
                case "220779":
                    tmpAreaid = "松原市(本级)";
                    break;
                case "220879":
                    tmpAreaid = "白城市(本级)";
                    break;
                case "222479":
                    tmpAreaid = "延边朝鲜族自治州(本级)";
                    break;
                case "222579":
                    tmpAreaid = "长白山管委会(本级)";
                    break;
                default:
                    break;
            }
            return tmpAreaid;
        }

    }

}
